import { createRouter, createWebHashHistory } from "vue-router"

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../../src/views/login/login.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../../src/views/home/home.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../../src/views/login/register.vue')
    },
    {
      path: '/allData',
      name: 'allData',
      component: () => import('../../src/views/login/allData.vue')
    },
    {
      path: '/reset',
      name: 'reset',
      component: () => import('../../src/views/login/reset.vue')
    },
    {
      path: '/resetPwd',
      name: 'resetPwd',
      component: () => import('../../src/views/login/resetPwd.vue')
    }
  ]
})

// 导航守卫，只有localStorage里有tolken的情况下，才能访问home页面
router.beforeEach((to, from, next) => {
  // console.log(to);
  const token = localStorage.getItem('token')
  if (!token && to.name === 'home') {
    next({
      name: 'login'
    })
  } else {
    next()
  }
})

export default router